/**
 * Distributed Retail System
 * Oct 1, 2012 
 */
package comp6231.project.client;

import java.util.Scanner;

import org.omg.CORBA.ORB;
import org.omg.CORBA.Object;
import org.omg.CORBA.ORBPackage.InvalidName;
import org.omg.CosNaming.NamingContextExt;
import org.omg.CosNaming.NamingContextExtHelper;
import org.omg.CosNaming.NamingContextHelper;

/**
 * @author dimitri.tiago
 *
 * This class executes a customer client. The customer client
 * invokes remote operations on remote retail store objects.
 */
public class CustomerClient 
{
	/**
	 * This method starts the execution of the customer client
	 * @param N/A
	 */
	public static void main(String[] args) 
	{	
		try 
		{
			//System.setSecurityManager(null);
			
			Scanner input = new Scanner(System.in);
		
			//TODO: handle cases where input mismatch exception occurs
		    // prompt user for CORBA registry port to use
		    System.out.println("Please enter CORBA Registry port number:");
		    int orbPort = input.nextInt();	
		    
			// obtain a new ORB instance
			String [] orbSettings = {"-ORBInitialHost", "localhost", "-ORBInitialPort", String.valueOf(orbPort)};
			ORB orb = ORB.init(orbSettings, null);
	    
			// obtain the root naming context
			org.omg.CORBA.Object namingContext = orb.resolve_initial_references("NameService");						// obtain the naming context CORBA object
			NamingContextExt namingContextExt  = (NamingContextExt) NamingContextExtHelper.narrow(namingContext);	// obtain java naming context extension
							
		    // create customer
		    //TODO: consider using nextLine(pattern) to limit input to valid customer ids.
		    System.out.println("Please enter customerID:");							// prompt user for customer id
		    String customerID 	   = input.next();
		    String retailStoresURL = String.format("!!localhost:%d!", orbPort);
		    Customer customer      = new Customer(customerID, retailStoresURL, namingContextExt);
		    
		    String customerOperation = "";											// present application menu
		    while ( ! (customerOperation.equalsIgnoreCase("Q")) )
		    {
			    // execute customer operation
			    System.out.println("Please enter a customer operation:"); 			// prompt user for customer operation
			    System.out.println("(B)	Buy");
			    System.out.println("(R)	Return");
			    System.out.println("(E)	Exchange");
			    System.out.println("(C)	Check Stock");
			    System.out.println("(Q)	Quit Application");
			    customerOperation = input.next();
			    
			   if (customerOperation.equalsIgnoreCase("B"))
			   {
				   // buy item(s)
				   System.out.println("Please enter an itemID to buy:"); 			// prompt user for itemId to buy
				   int itemID = input.nextInt();
				   System.out.println("Please enter number of items to buy:"); 		// prompt user for number of items to buy
				   int numberOfItems = input.nextInt();
				   String transactionResult = customer.Buy(itemID, numberOfItems); 	// buy items
				   System.out.printf("%s\n", transactionResult);
			   }
			   else if (customerOperation.equalsIgnoreCase("R"))
			   {
				   // return item(s)
				   System.out.println("Please enter an itemID to return:"); 				// prompt user for itemId to return
				   int itemID = input.nextInt();
				   System.out.println("Please enter number of items to return:"); 			// prompt user for number of items to return
				   int numberOfItems = input.nextInt();
				   String transactionResult = customer.returnItems(itemID, numberOfItems); 	// return items
				   System.out.printf("%s\n", transactionResult);
			   }
			   else if (customerOperation.equalsIgnoreCase("C"))
			   {
				   // check stock
				   System.out.println("Please enter an itemID:"); 					// prompt user for itemId to check stock
				   int itemID = input.nextInt();
			
				   String transactionResult = customer.checkStock(itemID); 			// check stock
				   System.out.printf("%s\n", transactionResult);
			   }
			   else if (customerOperation.equalsIgnoreCase("E"))
			   {
				   // exchange items
				   System.out.println("Please enter an itemID to exchange:"); 		// prompt user for itemId to exchange
				   int boughtItemID = input.nextInt();
				   
				   System.out.println("Please enter number of items exchange:");	// prompt user for number of items to exchange
				   int boughtNumber = input.nextInt();
				   
				   System.out.println("Please enter desired itemID:"); 				// prompt user for desired item id
				   int desiredItemID = input.nextInt();

				   System.out.println("Please enter desired number of items:");		// prompt user for desired number of items
				   int desiredNumber = input.nextInt();
				   
				   String transactionResult = customer.exchangeItems(boughtItemID, boughtNumber, desiredItemID, desiredNumber); // exchange items
				   System.out.printf("%s\n", transactionResult);
			   } 
		    }
		    
		    // close scanner
		    input.close();
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
		}
	}
}